#########Problems with code at NII research room
########Did balance checks manually

prop.test(x=c(as.numeric(153),as.numeric(15)), n=c(319,39))


#####
t.test2 <- function(m1,m2,s1,s2,n1,n2,m0=0,equal.variance=FALSE)
{
  if( equal.variance==FALSE ) 
  {
    se <- sqrt( (s1^2/n1) + (s2^2/n2) )
    # welch-satterthwaite df
    df <- ( (s1^2/n1 + s2^2/n2)^2 )/( (s1^2/n1)^2/(n1-1) + (s2^2/n2)^2/(n2-1) )
  } else
  {
    # pooled standard deviation, scaled by the sample sizes
    se <- sqrt( (1/n1 + 1/n2) * ((n1-1)*s1^2 + (n2-1)*s2^2)/(n1+n2-2) ) 
    df <- n1+n2-2
  }      
  t <- (m1-m2-m0)/se 
  dat <- c(m1-m2, se, t, 2*pt(-abs(t),df))    
  names(dat) <- c("Difference of means", "Std Error", "t", "p-value")
  return(dat) 
}

##Main
t.test2(3438.833,3939.4482,4923.261, 5691.907, 37293,113668)

t.test2(5833.0758,8825.4914,8232.538, 10469.4496, 37293,113668)

prop.test(x=c(13000,35715),n=c(37293,113668), correct=TRUE)
prop.test(x=c(5105,23325),n=c(37293,113668))

t.test2(3.2045,2.2567, 1.910025,1.2088, 37293,113668)

t.test2(7.2812,7.4152,4.584758,4.821, 37293,113668)

prop.test(x=c(30476,95072),n=c(37293,113668))
prop.test(x=c(19098,58187),n=c(37293,113668))


prop.test(x=c(14660,55368),n=c(37293,113668))
prop.test(x=c(10274,3910),n=c(37293,113668))
####matched###############################

t.test2(3438.833,3180.5377,4923.261, 4734.6885, 37293,23070)

t.test2(5833.0758,5645.1543,8232.538, 8134.39, 37293,23070)

prop.test(x=c(13000,7994),n=c(37293,23070), correct=TRUE)
prop.test(x=c(5105,2981),n=c(37293,23070))

t.test2(3.2045,3.1449, 1.910025,1.8306, 37293,23070)

t.test2(7.2812,7.2609,4.584758,4.6547, 37293,23070)

prop.test(x=c(30476,18636),n=c(37293,23070))
prop.test(x=c(19098,11780),n=c(37293,23070))


prop.test(x=c(14660,8658),n=c(37293,23070))
prop.test(x=c(10274,6556),n=c(37293,23070))


###########Arab population########################

t.test2(1312.7696,2490.8055, 3105.041,4134.7434, 14659,55362)

t.test2(5312.47188,7327.5416,6347.42, 8232.538,  14659,55362)

prop.test(x=c(1736,13381),n=c(14659,55362), correct=TRUE)
prop.test(x=c(1300,8033),n=c(14659,55362))

t.test2(3.7698,2.3666, 1.902164,1.1522, 14659,55362)

t.test2(7.8757,7.3503,4.328236,4.8255, 14659,55362)

prop.test(x=c(11460,51310),n=c(14659,55362))
prop.test(x=c(7483,28141),n=c(14659,55362))


############Arab matched#######################

t.test2(4371.4081,3314.8718, 4072.429,3693.5288, 10275,3915)

t.test2(2198.1199,3164.6718,4411.752, 5443.4437,  10275,3915)

prop.test(x=c(6362,2067),n=c(10275,3915), correct=TRUE)
prop.test(x=c(299,122),n=c(10275,3915))

t.test2(3.4632,3.13, 2.027762,1.8502, 10275,3915)

t.test2(6.15767,5.9011,4.59155,4.528, 110275,3915)

prop.test(x=c(9846,3634),n=c(14659,8901))
prop.test(x=c(5285,2065),n=c(14659,89012))


#####################Ultra Orthodox original################

t.test2(4371.4081,3314.8718, 4072.429,3693.5288, 10275,3915)

t.test2(2198.1199,3164.6718,4411.752, 5443.4437,  10275,3915)

prop.test(x=c(6362,2067),n=c(10275,3915), correct=TRUE)
prop.test(x=c(299,122),n=c(10275,3915))

t.test2(3.4632,3.13, 2.027762,1.8502, 10275,3915)

t.test2(6.15767,5.9011,4.59155,4.528, 110275,3915)

prop.test(x=c(9846,3634),n=c(14659,8901))
prop.test(x=c(5285,2065),n=c(14659,89012))

#####################Ultra Orthodox matched################

t.test2(4371.4081,4392.9045, 4072.429,4577.1327, 10275,3054)

t.test2(2198.1199,3164.6718,4411.752, 4270.0201,   10275,3054)

prop.test(x=c(6362,1942),n=c( 10275,3054), correct=TRUE)
prop.test(x=c(299,91),n=c( 10275,3054))

t.test2(3.4632,3.13, 2.027762,1.9834,  10275,3054)

t.test2(6.15767,6.158,4.59155,4.5051,  10275,3054)

prop.test(x=c(9846,2930),n=c( 10275,3054))
prop.test(x=c(5285,1584),n=c( 10275,3054))

#####################non miority original################

t.test2(5184.634,5458.8992, 6227.42,6696.9987, 12361,54392)

t.test2(9471.0532,10757.6048,10769.93, 12299.7487,   12361,54392)

prop.test(x=c(4902,20266),n=c( 12361,543924), correct=TRUE)
prop.test(x=c(3506,15170),n=c( 12361,54392))

t.test2(2.3189,2.082, 1.442166,1.1655,  12361,54392)

t.test2(7.5107,7.5903,4.707537,4.8162,  12361,54392)

prop.test(x=c(9169,40130),n=c( 12361,54392))
prop.test(x=c(6330,27974),n=c( 12361,54392))

#####################non minority matched################

t.test2(5184.634,5083.7682, 6227.42,6260.5257, 12361,10489)

t.test2(9471.0532,9716.4305,10769.93, 11414.2093,   12361,10489)

prop.test(x=c(4902,4062),n=c( 12361,10489), correct=TRUE)
prop.test(x=c(3506,2897),n=c( 12361,10489))

t.test2(2.3189,2.3494, 1.442166,1.4427,  12361,10489)

t.test2(7.5107,7.6614,4.707537,4.734,  12361,10489)

prop.test(x=c(9169,7719),n=c( 12361,10489))
prop.test(x=c(6330,5352),n=c( 12361,10489))

##Main survey original data
t.test2(4085.3103,6064.4863,5136.899, 8334.0248, 593,5285)

t.test2(5999.1383,11763.7447,8164.302, 13266.6509, 593,5285)

prop.test(x=c(205,2054),n=c(593,5285), correct=TRUE)
prop.test(x=c(70,1285),n=c(593,5285))

t.test2(4.7352,3.3561, 2.329828,1.9932, 593,5285)

t.test2(7.1096,8.6119,5.36818,5.8973, 593,5285)

prop.test(x=c(532,4762),n=c(593,5285))
prop.test(x=c(299,2707),n=c(593,5285))


prop.test(x=c(253,1183),n=c(593,5285))
prop.test(x=c(196,841),n=c(593,5285))

##Main survey - matched sample
t.test2(4085.3103,3945.6728,5136.899, 4646.3673, 593,483)

t.test2(5999.1383,6286.1956,8164.302, 8081.2377, 593,483)

prop.test(x=c(205,173),n=c(593,483), correct=TRUE)
prop.test(x=c(70,46),n=c(593,483))

t.test2(4.7352,5.1332, 2.329828,2.8888, 593,483)

t.test2(7.1096,6.6998,5.36818,5.5632, 593,483)

prop.test(x=c(532,447),n=c(593,483))
prop.test(x=c(299,238),n=c(593,483))


prop.test(x=c(253,164),n=c(593,483))
prop.test(x=c(196,183),n=c(593,483))

##Arab population survey data - matched
t.test2(1830.6166,3258.0491,3900.761, 4705.8711, 253,1182)

t.test2(4636.498,8117.1836,5519.732, 7457.0707,  253,1182)

prop.test(x=c(37,289),n=c( 253,1182), correct=TRUE)
prop.test(x=c(22,170),n=c( 253,1182))

t.test2(5.2016,3.0457, 1.948462,1.2115,  253,1182)

t.test2(7.6877,9.2724,5.480243,5.8196,  253,1182)

prop.test(x=c(212,1147),n=c( 253,1182))
prop.test(x=c(119,595),n=c( 253,1182))

##Ultra orthodox population survey data - matched
t.test2(1830.6166,922.2925,3900.761, 2312.5654, 253,1182)

t.test2(4636.498,8117.1836,5519.732, 7457.0707,  253,1182)

prop.test(x=c(37,289),n=c( 253,1182), correct=TRUE)
prop.test(x=c(22,170),n=c( 253,1182))

t.test2(5.2016,3.0457, 1.948462,1.2115,  253,1182)

t.test2(7.6877,9.2724,5.480243,5.8196,  253,1182)

prop.test(x=c(212,1147),n=c( 253,1182))
prop.test(x=c(119,595),n=c( 253,1182))

